// pages/addProduct/paySuccess/paySuccess.js
const utilService = require('../../../utils/utilService.js');
const service = require('../../../pages/mine/mineService.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsData: {},
    shareMsg: "",
    lefBtnTitle: "查看详情",
    result: 1, //支付结果 0失败 1成功
    fromPage: 1,//1商品详情
    orderId: "",//订单id
    desc:"",
    type: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let type = options.type
    if(type == 1){
      this.getShareMessage(19)
      this.setData({
        goodsData: JSON.parse(options.goodsData),
        lefBtnTitle: "前往查看",
        type: options.type,
        desc: "恭喜您成功领取现金红包，前往钱包查看！"
      })
      return
    }
    this.getShareMessage(20)
    if (options.goodsData) {
      this.setData({
        goodsData: JSON.parse(options.goodsData),
        orderId: options.orderId,
        result: options.result,
        desc: options.result == 1 ? '您的订单已经支付成功，请等待货主发货' : '订单支付失败，请联系客服或重新支付'
      })
    }
  },
  //获取分享信息
  getShareMessage: function (type) {
    var self = this;
    utilService.shareSettings(type,
      function (data) {
        console.log("分享数据", data)
        self.setData({
          shareMsg: data.title
        })
      },
      function (error) {

      }
    );
  },

  returnHome: function (e) {
    wx.switchTab({
      url: '../../home/home'
    })
  },
  checkClick: function (e) {
    if (this.data.type == 1){
      wx.navigateTo({
        url: "/withdrawCash/pages/withdrawCash/withdrawCash" + "?title=" + "提现",
      })
      return
    }
    let that = this;
    console.log(this.data.goodsData.goodsId);
    console.log("orderId:" + this.data.orderId);
    wx.navigateTo({
      url: '/pages/orderDetails/OrderDetails?order_id=' + that.data.orderId,
    })

  },
  showTost: function (str) {
    wx.showToast({
      title: str,
      icon: 'succes',
      duration: 1000,
      mask: true
    })
  },
  onShareAppMessage: function (res) {
    // console.log('1234456' + this.sharedData)
    let that = this
    var title = this.data.shareMsg

    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: title,//'看看我在【趣闲】平台上发布了啥闲置物品',
      path: 'goods/pages/goodsDetails/goodsDetails?goods_type=' + that.data.goodsData.saleTypeId + '&goods_id=' + that.data.goodsData.goodsId + "&userId=" + getApp().globalData.userId,
      imageUrl: that.data.goodsData.goodsUrl,
      success: function (res) {
        // 转发成功
        that.showTost('转发成功');
      },
      fail: function (res) {
        // 转发失败
        that.showTost('转发失败');
      }
    }
  }
})